#include <bits/stdc++.h>
#define int long long
using namespace std;

int b[100001], c[100001], a[100001], x[4];
char d[20][50001];
vector<int> winner[20][50001];
signed main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> b[i];
    for (int i = 1; i <= m; i++) cin >> c[i];
    int k = ceil(log2(n));
    for (int i = 1; i <= k; i++)
    {
        for (int j = 1; j <= pow(2, k - i); j++)
        {
            cin >> d[i][j];
            d[i][j] -= '0';
        }
    }
    int t;
    cin >> t;
    while (t--)
    {
        memset(a, 0, sizeof a);
        for (int i = 0; i < 4; i++) cin >> x[i];
        for (int i = 1; i <= n; i++) a[i] = b[i] ^ x[i % 4];
        int finalans = 0;
        for (int i = 1; i <= m; i++)
        {
            int maxk = ceil(log2(c[i]));
            for (int j = c[i] + 1; j <= pow(2, maxk); j++) a[j] = -1;
            for (int j = 1; j <= pow(2, maxk); j++)
            {
                winner[0][j].clear();
                winner[0][j].push_back(j);
            }
            for (int rnd = 1; rnd <= maxk; rnd++)
            {
                for (int test = 1; test <= pow(2, maxk - rnd); test++)
                {
                    winner[rnd][test].clear();
                    vector<int> first = winner[rnd - 1][2 * test - 1];
                    vector<int> second = winner[rnd - 1][2 * test];
                    int dd = d[rnd][test];
                    if (dd == 0)
                    {
                        bool flagwin = false;
                        bool flaglose = false;
                        for (int ffirst : first)
                        {
                            if (a[ffirst] != -1 && a[ffirst] >= rnd) flagwin = true;
                            if (a[ffirst] != -1 && a[ffirst] < rnd) flaglose = true;
                            if (a[ffirst] == -1) flagwin = flaglose = true;
                        }
                        if (flagwin && !flaglose)
                        {
                            for (int ffirst : first)
                            {
                                if (a[ffirst] != -1 && a[ffirst] >= rnd || a[ffirst] == -1)
                                {
                                    winner[rnd][test].push_back(ffirst);
                                }
                            }
                        }
                        if (flaglose && !flagwin) winner[rnd][test] = second;
                        if (flagwin && flaglose)
                        {
                            for (int ffirst : first)
                            {
                                if (a[ffirst] != -1 && a[ffirst] >= rnd || a[ffirst] == -1)
                                {
                                    winner[rnd][test].push_back(ffirst);
                                }
                            }
                            for (int ssecond : second) winner[rnd][test].push_back(ssecond);
                        }
                    }
                    else
                    {
                        bool flagwin = false;
                        bool flaglose = false;
                        for (int ssecond : second)
                        {
                            if (a[ssecond] != -1 && a[ssecond] >= rnd) flagwin = true;
                            if (a[ssecond] != -1 && a[ssecond] < rnd) flaglose = true;
                            if (a[ssecond] == -1) flagwin = flaglose = true;
                        }
                        if (flagwin && !flaglose)
                        {
                            for (int ssecond : second)
                            {
                                if (a[ssecond] != -1 && a[ssecond] >= rnd || a[ssecond] == -1)
                                {
                                    winner[rnd][test] = second;
                                }
                            }
                        }
                        if (flaglose && !flagwin) winner[rnd][test] = first;
                        if (flagwin && flaglose)
                        {
                            for (int ffirst : first) winner[rnd][test].push_back(ffirst);
                            for (int ssecond : second)
                            {
                                if (a[ssecond] != -1 && a[ssecond] >= rnd || a[ssecond] == -1)
                                {
                                    winner[rnd][test].push_back(ssecond);
                                }
                            }
                        }
                    }
                }
            }
            int sum = 0;
            for (int finalwinner : winner[maxk][1]) sum += finalwinner;
            finalans ^= (i * sum);
        }
        cout << finalans << endl;
    }
    return 0;
}
